package cn.iocoder.yudao.module.blog.controller.admin.poster;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.poster.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.poster.PosterDO;
import cn.iocoder.yudao.module.blog.convert.poster.PosterConvert;
import cn.iocoder.yudao.module.blog.service.poster.PosterService;

@Tag(name = "管理后台 - 海报")
@RestController
@RequestMapping("/blog/poster")
@Validated
public class PosterController {

    @Resource
    private PosterService posterService;

    @PostMapping("/create")
    @Operation(summary = "创建海报")
    @PreAuthorize("@ss.hasPermission('blog:poster:create')")
    public CommonResult<Long> createPoster(@Valid @RequestBody PosterCreateReqVO createReqVO) {
        return success(posterService.createPoster(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新海报")
    @PreAuthorize("@ss.hasPermission('blog:poster:update')")
    public CommonResult<Boolean> updatePoster(@Valid @RequestBody PosterUpdateReqVO updateReqVO) {
        posterService.updatePoster(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除海报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:poster:delete')")
    public CommonResult<Boolean> deletePoster(@RequestParam("id") Long id) {
        posterService.deletePoster(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得海报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:poster:query')")
    public CommonResult<PosterRespVO> getPoster(@RequestParam("id") Long id) {
        PosterDO poster = posterService.getPoster(id);
        return success(PosterConvert.INSTANCE.convert(poster));
    }

    @GetMapping("/list")
    @Operation(summary = "获得海报列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:poster:query')")
    public CommonResult<List<PosterRespVO>> getPosterList(@RequestParam("ids") Collection<Long> ids) {
        List<PosterDO> list = posterService.getPosterList(ids);
        return success(PosterConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得海报分页")
    @PreAuthorize("@ss.hasPermission('blog:poster:query')")
    public CommonResult<PageResult<PosterRespVO>> getPosterPage(@Valid PosterPageReqVO pageVO) {
        PageResult<PosterDO> pageResult = posterService.getPosterPage(pageVO);
        return success(PosterConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出海报 Excel")
    @PreAuthorize("@ss.hasPermission('blog:poster:export')")
    @OperateLog(type = EXPORT)
    public void exportPosterExcel(@Valid PosterExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PosterDO> list = posterService.getPosterList(exportReqVO);
        // 导出 Excel
        List<PosterExcelVO> datas = PosterConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "海报.xls", "数据", PosterExcelVO.class, datas);
    }

}
